'use strict';
// Copyright 2014 Blueant Inc. All Rights Reserved.

/**
 * @fileoverview 副本体力购买
 * @author sundbcn@gmail.com (sun debing)
 */

var _ = require('lodash');
var async = require('async');
var Code = require('../../../consts/code');
var moduleType = require('../../../consts/moduleType');
var operationType = require('../../../consts/operationType');
var roleService = require('../../../services/roleService');
var rewardService = require('../../../services/rewardService');
var PlayerManager = require('../../../libs/playerManager');
var constantUtil = require('../../../util/constantUtil');
var buySpConf = require('../../../config/buySpConf');
var vipConf = require('../../../config/vipConf');
var taskService = require('../../../services/taskService.js');

module.exports = function (app) {
    return new Handler(app);
};

var Handler = function (app) {
    this.app = app;
};

/**
 * 副本体力购买
 */
Handler.prototype.buy = function (msg, session, next) {
    var role_id = session.uid;
    var player_manager = new PlayerManager(role_id);
    var vip_buy_num;
    var has_buy_sp_num;

    async.waterfall(
        [
            function (cb) {
                player_manager.refreshData(cb);
            },
            function (cb) {
                player_manager.getModel(
                    ['role', 'bag', 'accrued', 'task', 'daily_task', 'achieve'],
                    cb);
            },
            function (cb) {
                var role_model = player_manager.role;

                // 是否有购买体力次数
                vip_buy_num = vipConf.getCanBuySpMaxNum(role_model.getVip());
                if (role_model.getBuySpNum() >= vip_buy_num) {
                    return next(null, {
                        code: Code.BAG_SP_BUY_NUM_MAX
                    });
                }

                // 扣除钻石
                var sub_diamond = buySpConf.getConf(role_model.getBuySpNum());
                if (!roleService.subDiamond(player_manager, moduleType.MDL_COPY, operationType.OPR_COPY_BUY_ENERGY, sub_diamond)) {
                    // 钻石够不够
                    return next(null, {
                        code: Code.USER_DIAMOND_NOT_ENOUGH
                    });
                }

                // 加入购买次数
                role_model.addBuySpNum();
                has_buy_sp_num = role_model.getBuySpNum();

                // 增加体力
                var buy_sp_num = constantUtil.value('buy_sp_num');
                roleService.addEnergy(player_manager, moduleType.MDL_COPY, operationType.OPR_COPY_BUY_ENERGY, buy_sp_num);
                taskService.addCompleteBuyNumEnergy(player_manager);
                player_manager.save(cb);
            }
        ],
        function (err) {
            if (!!err) {
                console.error(err);
                return next(null, {
                    code: Code.FAIL
                });
            }
            return next(null, {
                code: Code.OK,
                result: {
                    vip_buy_num: vip_buy_num,
                    has_buy_sp_num: has_buy_sp_num
                }
            });
        }
    );
};
